package com.code.google.mdsc.io.xml.impl.threading;

import java.io.File;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;
import java.util.Vector;

import javax.management.modelmbean.XMLParseException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.code.google.mdsc.gui.MainWindow;
import com.code.google.mdsc.io.AbstractTask;
import com.code.google.mdsc.io.log.SimpleLogger;
import com.code.google.mdsc.io.log.impl.SimpleLoggerImpl;
import com.code.google.mdsc.io.xml.XMLSepaGenerator;
import com.code.google.mdsc.io.xml.impl.XMLSepaGeneratorImpl;
import com.code.google.mdsc.model.Booking;
import com.code.google.mdsc.model.Creditor;
import com.code.google.mdsc.model.Debtor;
import com.code.google.mdsc.model.impl.BookingImpl;
import com.code.google.mdsc.model.impl.DebtorImpl;


public class XMLSepaGeneratorTask extends AbstractTask {

	private static SimpleLogger logger = SimpleLoggerImpl.getInstance();
	private final Booking regainedData;

	public XMLSepaGeneratorTask(File selectedFile, MainWindow mw,
			Creditor creditorData) throws XMLParseException {
		super(selectedFile, mw);
		this.regainedData = regainDataFromTableModel((DefaultTableModel) mw
				.getTable().getModel(), creditorData);
	}

	@Override
	protected void doAfterDone() {
		// empty template method
	}

	@Override
	protected void doIOOperations() {

		XMLSepaGenerator xmlGenerator = new XMLSepaGeneratorImpl(this);

		xmlGenerator.generate(getSelectedFile(), regainedData);

	}

	@SuppressWarnings("rawtypes")
	private Booking regainDataFromTableModel(DefaultTableModel dm,
			Creditor creditorData) throws XMLParseException {

		// TODO check if this works correctly
		List<Debtor> debtors = new ArrayList<Debtor>();
		GregorianCalendar bookingDate = new GregorianCalendar();

		Vector dataVector = dm.getDataVector();

		DateFormat sdf = SimpleDateFormat.getDateInstance();
		for (int i = 0; i < dataVector.size(); i++) {

			try {
				Vector v = (Vector) dataVector.elementAt(i);

				String mandatoryNumber = (String) v.elementAt(0);
				String accountHolder = (String) v.elementAt(1);
				BigDecimal bookingVolumne = (BigDecimal) v.elementAt(2);
				GregorianCalendar mandatorySignatureDate = new GregorianCalendar();
				mandatorySignatureDate.setTimeZone(TimeZone.getTimeZone("CET"));
				mandatorySignatureDate.setTime(sdf.parse((String) v
						.elementAt(3)));
				String unstructuredPurpose = (String) v.elementAt(4);
				String iban = (String) v.elementAt(5);
				String bic = (String) v.elementAt(6);
				bookingDate.setTimeZone(TimeZone.getTimeZone("CET"));
				bookingDate.setTime(sdf.parse((String) v.elementAt(7)));

				String sequenceType = (String) v.elementAt(8);
				Debtor regainedDebtor = new DebtorImpl(bic, iban,
						mandatoryNumber, accountHolder, bookingVolumne,
						mandatorySignatureDate, unstructuredPurpose,
						sequenceType);

				debtors.add(regainedDebtor);
			} catch (ParseException e) {
				logger.log(SimpleLogger.LogMode.ERROR, e.getMessage());
				throw new XMLParseException(e,
						"Parsing a date while regaining table model data failed");
			}
		}
		return new BookingImpl(creditorData, debtors, bookingDate);
	}

	@Override
	protected void showDialog() {
		JOptionPane.showMessageDialog(getMw().getMainFrame(),
				"saving the xml data finished...");

	}
}
